import { useUserStore } from "@/stores/user";

export const permission = {
  mounted(el, binding) {
    const userStore = useUserStore();
    const { value } = binding;
    const permissionList = userStore.permissions; // 直接访问 store 中的 state

    if (value && value instanceof Array && value.length > 0) {
      const permissionRoles = value;

      const hasPermission = permissionRoles.some((role) => {
        return permissionList.includes(role);
      });

      if (!hasPermission) {
        el.parentNode && el.parentNode.removeChild(el);
      }
    }
  }
};

// 导出注册函数
export function setupPermission(app) {
  app.directive('permission', permission);
}
